﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;

using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.DataAccess;
namespace BySoft.Server.Business
{
    public partial class SysRole
    {
        public static DataTable GetRoleList(string roleName, int pageIndex, int pageSize, out int count)
        {
            return SysRoleDA.GetRoleList(roleName,pageIndex, pageSize, out count);
        }

        public static DataTable GetRoleListWithoutUserTypeRole(string roleName, int pageIndex, int pageSize, out int count)
        {
            return SysRoleDA.GetRoleListWithoutUserTypeRole(roleName, pageIndex, pageSize, out count);
        }

        public static bool InsertSysRole(SysRoleEntity objSysRoleEntity, string sequenceName, string entityField)
        {
            if (objSysRoleEntity == null || String.IsNullOrEmpty(sequenceName) || String.IsNullOrEmpty(entityField))
            {
                return false;
            }
            try
            {
                return DataAccessCommon.Insert(objSysRoleEntity, sequenceName, entityField);
            }
            catch
            {
                throw;
            }

        }

        /// <summary>
        /// 获取用户和工作组
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="selectGroup">查询的数据中是否包含工作组数据</param>
        /// <returns></returns>
        public static DataTable GetMemberForRole(string id, string name, int pageIndex, int pageSize, out int count)
        {
            return SysRoleDA.GetMemberForRole(id, name,  pageIndex, pageSize, out count);
        }

        /// <summary>
        /// 获取角色用户
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public static DataTable GetRoleMember(string roleId)
        {
            return SysRoleDA.GetRoleMember(roleId);
        }

        /// <summary>
        /// 删除角色成员
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="memberId"></param>
        public static void DeleteRoleMember(string roleId)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    SysRoleDA.DeleteUserRole(roleId, broker);

                    //SysRoleDA.DeleteWorkinggroupRole(roleId, broker);
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    broker.Rollback();
                    throw ex;
                }
                
            }
        }

        public static void DeleteRoleMember(string roleId, DataAccessBroker broker)
        {
            
                SysRoleDA.DeleteUserRole(roleId, broker);

                //SysRoleDA.DeleteWorkinggroupRole(roleId, broker);
              
        }

        public static int DeleteRoleRights(string roleId)
        {
            return SysRoleDA.DeleteRoleRights(roleId);
        }

        public static SysRoleExEntity GetSysRoleExEntity(string roleId)
        {
            return SysRoleDA.GetSysRoleExEntity(roleId);
        }

        public static DataTable GetRoleByRoleName(string roleName)
        {
            return SysRoleDA.GetRoleByRoleName(roleName);
        }

        public static bool AddRole(SysRoleEntity oSysRole, List<SysRolerightsEntity> listSysRolerights, List<SysUserroleEntity> listSysUserrole)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();

                    DataAccessCommon.Insert(oSysRole,true, broker);

                    foreach (SysRolerightsEntity oSysRolerights in listSysRolerights)
                    {
                        oSysRolerights.RoleID = oSysRole.RoleID.Value;
                        DataAccessCommon.Insert(oSysRolerights, true, broker);
                    }
                    foreach (SysUserroleEntity oSysUserrole in listSysUserrole)
                    {
                        oSysUserrole.RoleID = oSysRole.RoleID.Value;
                        DataAccessCommon.Insert(oSysUserrole, true, broker);
                    }
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
               
            }
            return isResult;
        }

        public static bool UpdateRole(SysRoleEntity oSysRole, List<SysRolerightsEntity> listSysRolerights, List<SysUserroleEntity> listSysUserrole, List<SysUserroleEntity> listSysUserroleDel)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();

                    DataAccessCommon.Update(oSysRole, broker);
                    SysRoleDA.DeleteRoleRights(oSysRole.RoleID.Value.ToString(), broker);
                    //DeleteRoleMember(oSysRole.Roleid.Value.ToString(), broker);

                    foreach (SysRolerightsEntity oSysRolerights in listSysRolerights)
                    {
                        oSysRolerights.RoleID = oSysRole.RoleID.Value;
                        DataAccessCommon.Insert(oSysRolerights, true, broker);
                    }
                    //foreach (SysWorkinggrouproleEntity oSysWorkinggrouprole in listSysWorkinggrouprole)
                    //{
                    //    oSysWorkinggrouprole.Roleid = oSysRole.Roleid.Value;
                    //    DataAccessCommon.Insert(oSysWorkinggrouprole, "WORKINGGROUPROLE_SEQU", "Workinggrouproleid", broker);
                    //}
                    foreach (SysUserroleEntity oSysUserrole in listSysUserrole)
                    {
                        oSysUserrole.RoleID = oSysRole.RoleID.Value;
                        DataAccessCommon.Insert(oSysUserrole, true, broker);
                    }

                    //foreach (SysWorkinggrouproleEntity oSysWorkinggrouprole in listSysWorkinggrouproleDel)
                    //{
                    //    DataAccessCommon.Delete(oSysWorkinggrouprole);
                    //}
                    foreach (SysUserroleEntity oSysUserrole in listSysUserroleDel)
                    {
                        DataAccessCommon.Delete(oSysUserrole);
                    }

                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
                
            }
            return isResult;
        }

        public static bool IsContainUser(string roleId)
        {
            return SysRoleDA.IsContainUser(roleId);
        }

        public static bool HavePower(string strUserId, string strRoleName)
        {
            return SysRoleDA.HavePower(strUserId, strRoleName);
        }
    }
}
